# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=spdlog
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.15.3
pkgrel=1
pkgdesc='Very fast, header-only/compiled, C++ logging library (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/gabime/spdlog'
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-catch"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-fmt")
source=("https://github.com/gabime/spdlog/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        001-spdlog_fmt_external.patch)
sha256sums=('15a04e69c222eb6c01094b5c7ff8a249b36bb22788d72519646fb85feb267e67'
            'a0eb34b7c6920f0db2587460071f53372663c191cdfe34bf5ea2704c309c745f')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # copied from https://gitlab.archlinux.org/archlinux/packaging/packages/spdlog/-/blob/189f1145/spdlog_fmt_external.patch
  apply_patch_with_msg \
    001-spdlog_fmt_external.patch

  find . -name "*.orig" -exec rm -f {} \;
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DSPDLOG_BUILD_BENCH=OFF \
    -DSPDLOG_BUILD_EXAMPLE=OFF \
    -DSPDLOG_BUILD_TESTS=OFF \
    -DSPDLOG_FMT_EXTERNAL=ON \
    -DSPDLOG_BUILD_SHARED=ON \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DCMAKE_RUNTIME_OUTPUT_DIRECTORY="${srcdir}/build-${MSYSTEM}" \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/cmake -DSPDLOG_BUILD_TESTS=ON ../${_realname}-${pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .

  ${MINGW_PREFIX}/bin/ctest || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
}
